package com.sky.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {


    @Autowired
    private  SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 根据套餐id查询菜品list
     * @param id
     * @return
     */
    public List<DishItemVO> getDishById(Long id) {

        // select * from setmeal_dish sd,dish d where sd.dish_id=d.id and id=?
        List<DishItemVO> dishItemVOList = setmealMapper.getDishItemByid(id);
        return dishItemVOList;
    }

    /**
     * 根据分类id查询套餐列表
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> setmealList=  setmealMapper.list(setmeal.getCategoryId());
        return setmealList;

    }


    /**
     * 根据id查询套餐详情
     * @param id
     * @return
     */
    public SetmealVO getByIdAndDish(Long id) {
        if (id == null) {
            return null;
        }
        Setmeal setmeal=setmealMapper.getById(id);
        // 属性赋值
        SetmealVO setmealVO=new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);

        //获取套餐中的所有菜品
        List<SetmealDish> setmealDishes=setmealDishMapper.getBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 更新套餐
     * @param setmealDTO
     */
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        //1、修改套餐表，执行update
        setmealMapper.update(setmeal);

        // 获取套餐的id
        Long setmealId = setmealDTO.getId();

        // 2.更新套餐中的菜品

        //2.1. 删除套餐中的菜品
        setmealDishMapper.deleteSetmealDish(Collections.singletonList(setmealId));
        //2.2 添加套餐中的菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(dish -> dish.setSetmealId(setmealId));
        setmealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 启售/停售
     * @param id
     * @param status
     */
    public void startOrStop(Long id, Integer status) {
        // 起售套餐时，判断套餐内是否有停售菜品
        // 有停售菜品提示"套餐内包含未启售菜品，无法启售"
        if(status==StatusConstant.ENABLE) {
            List<Dish> dishList= setmealMapper.getDishByid(id);
            dishList.forEach(dish->{
                if(dish.getStatus()==StatusConstant.DISABLE){
                    throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });
        }

        // 判断套餐是否起售
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();

          //起售套餐时，如果套餐内包含停售的菜品，则不能起售
        setmealDishMapper.update(setmeal);
    }

    /**
     * 分页查询套餐
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        // PageHelper.startPage(当前页，每页显示条数);
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        // 查询数据
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);

        return new PageResult(page.getTotal(),page.getResult());
    }


    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @Transactional
    public void saveWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        // 向套餐表插入数据，返回主键值
        setmealMapper.save(setmeal);// useGeneratedKeys="true" keyProperty="id" parameterType="Setmeal">

        // 获取套餐的主键值
        Long setmealId = setmeal.getId();// insert语句带回id

        // 获取套餐中的菜品数据
        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
        // 判断菜品数据是否为空
        if (!setmealDishList.isEmpty()){
            setmealDishList.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);// 设置套餐id，setmeal_id
            });
            // 根据setmeal_id向setmeal_idish表中插入n条菜品
            setmealDishMapper.insertBatch(setmealDishList);
        }
    }

    /**
     * 删除套餐
     * @param ids
     */
    @Transactional
    public void delete(List<Long> ids) {
        // 起售中的套餐不能删除
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(id);
            if(setmeal.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        // 删除套餐
        setmealMapper.deleteSetmeal(ids);
        // 删除套餐中的setmealDish数据
        setmealDishMapper.deleteSetmealDish(ids);
    }




}
